﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PSAMMatrixCloud.Common
{
    public class SimpleResultHelper
    {
        /// <summary>
        /// 设置返回的结果集
        /// </summary>
        /// <param name="errorCode">错误编码</param>
        /// <param name="message">提示消息</param>
        /// <param name="data">数据</param>
        /// <param name="isSuccess">状态 false 失败 true 成功</param>
        /// <returns></returns>
        public static Common.SimpleResult SetData(string errorCode, string message = null, object data = null, bool isSuccess = false)
        {
            Common.SimpleResult ret = new Common.SimpleResult
            {
                Status = (isSuccess ? "1" : "0"),
                Message = message,
                ErrorCode = errorCode,
                Data = data
            };
            return ret;
        }

        /// <summary>
        /// 设置返回的结果集
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="errorCode">错误编码</param>
        /// <param name="data">数据</param>
        /// <param name="isSuccess">状态 false 失败 true 成功</param>
        /// <returns></returns>
        public static Common.SimpleResult SetData<T>(T obj, string errorCode, object data = null, bool isSuccess = false)
        {
            Common.SimpleResult ret = new Common.SimpleResult();
            string message = commonFunction.GetCustomAttrMsg<T>(obj, errorCode);
            ret.Status = (isSuccess ? "1" : "0");
            ret.Message = message;
            ret.ErrorCode = errorCode;
            ret.Data = data;
            return ret;
        }

        /// <summary>
        /// 设置返回成功的消息
        /// </summary>
        /// <param name="data"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static Common.SimpleResult SetDataForSuccess(object data = null, string message = null)
        {
            return SetData("", message, data, true);
        }
    }

}
